package com.ruoyi.web.controller.sts;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.sts.domain.StsDataSyn;
import com.ruoyi.sts.service.IStsDataSynService;
import com.ruoyi.sts.service.IStsDeviceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sts/data/syn")
public class StsDataSynController extends BaseController {

    private String prefix = "sts/syn";

    @Autowired
    private IStsDataSynService stsDataSynService;

    @Autowired
    private IStsDeviceService stsDeviceService;

    @RequestMapping(value = "/syn")
    @ResponseBody
    public R syn(@RequestBody String xml){
        StsDataSyn stsDataSyn = new StsDataSyn();
        stsDataSyn.setData(xml);
        stsDataSyn.setSynFlag("N");
        stsDataSyn.setSynNum(0);
        stsDataSynService.insertStsDataSyn(stsDataSyn);
        stsDeviceService.updateLastSendTime(xml);
        return R.ok();
    }

    @RequiresPermissions("sts:syn:view")
    @GetMapping()
    public String syn()
    {
        return prefix + "/syn";
    }

    /**
     * 查询客流信息同步列表
     */
    @RequiresPermissions("sts:syn:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StsDataSyn stsDataSyn)
    {
        startPage();
        List<StsDataSyn> list = stsDataSynService.selectStsDataSynList(stsDataSyn);
        return getDataTable(list);
    }

    /**
     * 导出客流信息同步列表
     */
    @RequiresPermissions("sts:syn:export")
    @Log(title = "客流信息同步", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StsDataSyn stsDataSyn)
    {
        List<StsDataSyn> list = stsDataSynService.selectStsDataSynList(stsDataSyn);
        ExcelUtil<StsDataSyn> util = new ExcelUtil<StsDataSyn>(StsDataSyn.class);
        return util.exportExcel(list, "客流信息同步数据");
    }

    /**
     * 新增客流信息同步
     */
    @RequiresPermissions("sts:syn:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存客流信息同步
     */
    @RequiresPermissions("sts:syn:add")
    @Log(title = "客流信息同步", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StsDataSyn stsDataSyn)
    {
        return toAjax(stsDataSynService.insertStsDataSyn(stsDataSyn));
    }

    /**
     * 修改客流信息同步
     */
    @RequiresPermissions("sts:syn:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        StsDataSyn stsDataSyn = stsDataSynService.selectStsDataSynById(id);
        mmap.put("stsDataSyn", stsDataSyn);
        return prefix + "/edit";
    }

    /**
     * 修改保存客流信息同步
     */
    @RequiresPermissions("sts:syn:edit")
    @Log(title = "客流信息同步", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StsDataSyn stsDataSyn)
    {
        return toAjax(stsDataSynService.updateStsDataSyn(stsDataSyn));
    }

    /**
     * 删除客流信息同步
     */
    @RequiresPermissions("sts:syn:remove")
    @Log(title = "客流信息同步", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stsDataSynService.deleteStsDataSynByIds(ids));
    }
}
